package com.sunxiansheng.exception.strategys;

import com.sunxiansheng.exception.CustomException;
import com.sunxiansheng.exception.response.Result;
import org.springframework.stereotype.Component;

/**
 * Description: 自定义异常具体策略
 * @Author sun
 * @Create 2024/7/11 18:09
 * @Version 1.0
 */
@Component
public class CustomExceptionStrategy implements ExceptionStrategy{

    @Override
    public boolean supports(Exception e) {
        // 如果传进来的时自定义异常的对象，就返回true
        return e instanceof CustomException;
    }

    @Override
    public Result handleException(Exception e) {
        // 转换为自定义异常
        CustomException ex = (CustomException) e;
        // 根据自定义异常的枚举，构建response
        return Result.fail(ex.getRespBeanEnum());
    }

}